package com.example.txavdemo

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import com.pince.lifcycleliveav.em.PlayAutoAdjust
import com.pince.lifcycleliveav.em.*
import com.pince.lifcycleliveav.pullPalyer.ILivePlayer
import com.pince.lifcycleliveav.pullPalyer.IPullPlayController
import com.pince.lifcycleliveav.pullPalyer.LifcycleTXLivePlayer

class PlayerActivity : Activity() {
    companion object {
        fun startPlayerActivity(context: Context) {
            context.startActivity(Intent(context, PlayerActivity::class.java))
        }
    }

    //    var playUrl = "http://zsplay.jzzhibo.com/live/zs_1_2029486.flv"
//    var playUrl = "rtmp://3891.liveplay.myqcloud.com/live/3891_testclock_rtmpacc?bizid=3891&txSecret=c8d09333cf798d12c3d06ed7dc296d12&txTime=5F07E7BE"
//    var playUrl = "rtmp://3891.liveplay.myqcloud.com/live/3891_testclock_rtmpacc?bizid=3891&txSecret=a84a3dea406bfcc9d73dfc7b435a0894&txTime=5F6089DD"

        var playUrl = "http://zsplay.hzxingman.com/live/zs_1_2673707.flv"
    var livePlayer: LifcycleTXLivePlayer? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_player)
        livePlayer = findViewById<LifcycleTXLivePlayer>(R.id.livePlayer)
        val btnQuicknessMODEL = findViewById<Button>(R.id.btnQuicknessMODEL)
        val btnAutoMDOEL = findViewById<Button>(R.id.btnAutoMDOEL)
        val btnFluencyMODEL = findViewById<Button>(R.id.btnFluencyMODEL)
        val btnStart = findViewById<Button>(R.id.btn_start)
        val editUrl = findViewById<EditText>(R.id.edit_url)
        val log = findViewById<TextView>(R.id.log)
        log.movementMethod = ScrollingMovementMethod.getInstance();
        livePlayer?.playAutoAdjust = PlayAutoAdjust.QuicknessMODEL
        livePlayer?.setTXLivePlayConfig(false)
        livePlayer?.startPlay(playUrl)
        livePlayer?.setRenderMode(RENDER_MODE_ADJUST_RESOLUTION)
        livePlayer?.addController(object : IPullPlayController {
            override fun attachPlayer(livePlayer: ILivePlayer) {
            }

            override fun onNetStatus(bundle: Bundle) {
                Log.e("TAG", "" + bundle.toString())
                log.text = bundle.toString()
                        .replace("CPU_USAGE", "\n当前瞬时 CPU 使用率:")
                        .replace("VIDEO_WIDTH", "\n视频分辨率 - 宽:")
                        .replace("VIDEO_HEIGHT", "\n视频分辨率 - 高:")
                        .replace("NET_SPEED", "\n当前的网络的下载速度:")
                        .replace("VIDEO_FPS", "\n当前流媒体的视频帧率:")
                        .replace("VIDEO_BITRATE", "\n当前流媒体的视频码率，单位：kbps:")
                        .replace("AUDIO_BITRATE", "\n当前流媒体的音频码率，单位：kbps:")
                        .replace("VIDEO_CACHE", "\n视频播放缓冲区（jitterbuffer）大小，缓冲区越小越难以抵抗卡顿:")
                        .replace("AUDIO_CACHE", "\n音频播放缓冲区（jitterbuffer）大小，缓冲区越小越难以抵抗卡顿:")
                        .replace("SERVER_IP", "\n当前连接的服务器 IP:")
            }

            override fun onPlayEvent(event: Int, param: Bundle?) {
//                log.text = event.toString() + param?.toString()

            }

        })
        btnQuicknessMODEL.setOnClickListener {
            livePlayer?.playAutoAdjust = PlayAutoAdjust.QuicknessMODEL
            livePlayer?.setTXLivePlayConfig(false)
        }
        btnFluencyMODEL.setOnClickListener {
            livePlayer?.playAutoAdjust = PlayAutoAdjust.FluencyMODEL
            livePlayer?.setTXLivePlayConfig(false)
        }
        btnAutoMDOEL.setOnClickListener {
            livePlayer?.playAutoAdjust = PlayAutoAdjust.AutoMDOEL
            livePlayer?.setTXLivePlayConfig(false)
        }

        btnStart.setOnClickListener{
            livePlayer?.startPlay(editUrl.text.toString())
        }

    }

    override fun onResume() {
        super.onResume()
        livePlayer?.onResume()
    }

    override fun onPause() {
        super.onPause()
        livePlayer?.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        livePlayer?.onDestroy()
    }
}